package com.yugao.fintech.draper.multicache.support;

import com.yugao.fintech.draper.message.queue.support.broadcast.AbstractRedisBroadcastMessageListener;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;

@Slf4j
@RequiredArgsConstructor
public class CacheMessageConsumer extends AbstractRedisBroadcastMessageListener<CacheMessage> {
    private final CacheManager cacheManager;

    @Override
    public void onMessage(CacheMessage cacheMessage) {
        if (cacheManager instanceof RedisCaffeineCacheManager) {
            if (log.isDebugEnabled()) {
                log.debug("接收到redis topic消息，清空本地缓存，缓存名称:{}, key:{}", cacheMessage.getCacheName(),
                        cacheMessage.getKey());
            }
            RedisCaffeineCacheManager redisCaffeineCacheManager = (RedisCaffeineCacheManager) cacheManager;
            redisCaffeineCacheManager.clearLocal(cacheMessage.getCacheName(), cacheMessage.getKey());
        }
    }
}